package com.guli.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.commonutils.R;
import com.guli.edu.entity.Course;
import com.guli.edu.entity.vo.CourseInfoVo;
import com.guli.edu.entity.vo.CoursePublishVo;
import com.guli.edu.entity.vo.CourseQuery;
import com.guli.edu.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author liuzg
 * @since 2021-03-08
 */
@RestController
@RequestMapping("/edu/course")
@Api(tags = "课程信息管理")
public class CourseController {

    @Autowired
    private CourseService courseService;

    //课程列表 基本实现
    //TODO  完善条件查询带分页
    @ApiOperation(value = "课程列表 基本实现")
    @PostMapping(value = "/getList/{current}/{limit}")
    public R getCourseList(@PathVariable Long current, @PathVariable Long limit,
                           @RequestBody(required = false) CourseQuery courseQuery) {
        // 创建page对象
        Page<Course> pageCourse = new Page<>(current, limit);
        // 构造条件
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();
        if (!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        if (!StringUtils.isEmpty(status)){
            wrapper.eq("status",status);
        }
        //排序
        wrapper.orderByDesc("gmt_create");

        //调用方法实现条件查询分页
        courseService.page(pageCourse,wrapper);

        // 获取总记录数
        long total = pageCourse.getTotal();

        // 获取返回数据集合
        List<Course> records = pageCourse.getRecords();

        return R.ok().data("total",total).data("list",records);
    }



    @ApiOperation(value = "添加课程基本信息")
    @PostMapping(value = "/addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        String courseId = courseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",courseId);
    }


    @ApiOperation(value = "根据课程id查询课程基本信息")
    @GetMapping(value = "/getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId) {
        CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }


    @ApiOperation(value = "修改课程信息")
    @PostMapping(value = "/updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }


    @ApiOperation(value = "根据课程id查询课程确认信息")
    @GetMapping(value = "/getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id) {
        CoursePublishVo coursePublishVo = courseService.publishCourseInfo(id);
        return R.ok().data("publishCourse",coursePublishVo);
    }


    @ApiOperation(value = "课程最终发布")
    @PostMapping(value = "/publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        Course course = new Course();
        course.setId(id);
        course.setStatus("Normal"); // 设置课程发布状态
        courseService.updateById(course);
        return R.ok();
    }


    @ApiOperation(value = "删除课程")
    @DeleteMapping(value = "/remove/{courseId}")
    public R deleteCourse(@PathVariable String courseId) {
        courseService.removeCourse(courseId);
        return R.ok();
    }

}

